<?php

require_once __DIR__.'/vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

// 发布订阅模式
// 创建一个链接,指定用vhost为fanout
$arg = [
    'rabbitmq',
    'rabbitmq',
    'fanout',
];
$connection = new AMQPStreamConnection('localhost',5672,...$arg);
$channel = $connection->channel();

$exchange_name = "hello_exchange";
$queue_name = "phone_queue";
// 消费者关联队列
$channel->queue_declare($queue_name);
// 消费者绑定交换机,参数三routingKey
$channel->queue_bind($queue_name,$exchange_name);


$callback = function($msg) use($queue_name){
    echo $queue_name. ':',$msg->delivery_info['routing_key'], ':', $msg->body, "\n";
};

$channel->basic_consume($queue_name,'', false, true, false, false, $callback);

while($channel->is_consuming()) {
    $channel->wait();
}

// 关闭连接
$channel->close();
$connection->close();

function callBackFunc($result,$queue){
    $msg = $result->getBody();
    var_dump($msg);
}